{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"45%\" align=\"right\" border=\"4\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Derivatives Portfolio Risk Statistics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "From a risk management perspective it is important to know **how sensitive derivatives portfolios are** with regard to certain parameter values (market quotes, model assumptions, etc.). This part illustrates how to generate certain **risk reports** for `derivatives_portfolio` objects."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import dx\n",
    "import datetime as dt\n",
    "import time\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Risk Factors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The example is based on **two risk factors**, both modeled as geometric Brownian motions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# constant short rate\n",
    "r = dx.constant_short_rate('r', 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# market environment\n",
    "me_gbm_1 = dx.market_environment('gbm_1', dt.datetime(2015, 1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# geometric Brownian motion\n",
    "me_gbm_1.add_constant('initial_value', 40.)\n",
    "me_gbm_1.add_constant('volatility', 0.2) \n",
    "me_gbm_1.add_constant('currency', 'EUR')\n",
    "me_gbm_1.add_constant('model', 'gbm')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "me_gbm_2 = dx.market_environment('gbm_2', me_gbm_1.pricing_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# valuation environment\n",
    "val_env = dx.market_environment('val_env', dt.datetime(2015, 1, 1))\n",
    "val_env.add_constant('paths', 25000)\n",
    "    # 25,000 paths\n",
    "val_env.add_constant('frequency', 'W')\n",
    "    # weekly frequency\n",
    "val_env.add_curve('discount_curve', r)\n",
    "val_env.add_constant('starting_date', dt.datetime(2015, 1, 1))\n",
    "val_env.add_constant('final_date', dt.datetime(2015, 12, 31))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add valuation environment to market environments\n",
    "me_gbm_1.add_environment(val_env)\n",
    "me_gbm_2.add_environment(me_gbm_1)\n",
    "me_gbm_2.add_constant('initial_value', 40.)\n",
    "me_gbm_2.add_constant('volatility', 0.5)\n",
    "  # higher volatility"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "risk_factors = {'gbm_1' : me_gbm_1, 'gbm_2' : me_gbm_2}\n",
    "  # market with two risk factors"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Derivatives Positions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are going to model **total of 6 derivatives positions**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Market Environment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "All derivatives instruments (positions) share the same `market_environment` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# market environment for the options\n",
    "me_option = dx.market_environment('put', dt.datetime(2015, 1, 1))\n",
    "me_option.add_constant('maturity', dt.datetime(2015, 12, 31))\n",
    "me_option.add_constant('currency', 'EUR')\n",
    "me_option.add_environment(val_env)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Derivatives Positions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Two different kinds of derivatives make up the portfolio---an **American put option** and a **European maximum call option**. Both types of derivatives populate three positions, respectively."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "positions = {}\n",
    "half = 3  # 2 times that many options\n",
    "for i in range(half):\n",
    "    name = 'am_put_pos_%s' %i  # same name for position key and name\n",
    "    positions[name] = dx.derivatives_position(\n",
    "                        name=name,\n",
    "                        quantity=1,\n",
    "                        underlyings=['gbm_1'],\n",
    "                        mar_env=me_option,\n",
    "                        otype='American single',\n",
    "                        payoff_func='np.maximum(instrument_values - 40., 0)')\n",
    "\n",
    "multi_payoff = \"np.maximum(np.maximum(maturity_value['gbm_1'], maturity_value['gbm_2']) - 40., 0)\"\n",
    "for i in range(half, 2 * half):\n",
    "    name = 'multi_pos_%s' %i  # same name for position key and name\n",
    "    positions[name] = dx.derivatives_position(\n",
    "                        name=name,\n",
    "                        quantity=1,\n",
    "                        underlyings=['gbm_1', 'gbm_2'],\n",
    "                        mar_env=me_option,\n",
    "                        otype='European multi',\n",
    "                        payoff_func=multi_payoff)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Portfolio Modeling and Valuation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The instantiation of the `derivatives_portfolio` object is as usual."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "portfolio = dx.derivatives_portfolio(\n",
    "                        name='portfolio',\n",
    "                        positions=positions,\n",
    "                        val_env=val_env,\n",
    "                        risk_factors=risk_factors,\n",
    "                        correlations=None,\n",
    "                        parallel=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total\n",
      " pos_value    40.806\n",
      "dtype: float64\n",
      "CPU times: user 2.51 s, sys: 51.9 ms, total: 2.56 s\n",
      "Wall time: 644 ms\n"
     ]
    }
   ],
   "source": [
    "%time res = portfolio.get_values(fixed_seed=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here, the **value estimates** from the Monte Carlo simulation and valuation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>position</th>\n",
       "      <th>name</th>\n",
       "      <th>quantity</th>\n",
       "      <th>otype</th>\n",
       "      <th>risk_facts</th>\n",
       "      <th>value</th>\n",
       "      <th>currency</th>\n",
       "      <th>pos_value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>am_put_pos_0</td>\n",
       "      <td>am_put_pos_0</td>\n",
       "      <td>1</td>\n",
       "      <td>American single</td>\n",
       "      <td>[gbm_1]</td>\n",
       "      <td>3.325</td>\n",
       "      <td>EUR</td>\n",
       "      <td>3.325</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>am_put_pos_1</td>\n",
       "      <td>am_put_pos_1</td>\n",
       "      <td>1</td>\n",
       "      <td>American single</td>\n",
       "      <td>[gbm_1]</td>\n",
       "      <td>3.303</td>\n",
       "      <td>EUR</td>\n",
       "      <td>3.303</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>am_put_pos_2</td>\n",
       "      <td>am_put_pos_2</td>\n",
       "      <td>1</td>\n",
       "      <td>American single</td>\n",
       "      <td>[gbm_1]</td>\n",
       "      <td>3.275</td>\n",
       "      <td>EUR</td>\n",
       "      <td>3.275</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>multi_pos_3</td>\n",
       "      <td>multi_pos_3</td>\n",
       "      <td>1</td>\n",
       "      <td>European multi</td>\n",
       "      <td>[gbm_1, gbm_2]</td>\n",
       "      <td>10.301</td>\n",
       "      <td>EUR</td>\n",
       "      <td>10.301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>multi_pos_4</td>\n",
       "      <td>multi_pos_4</td>\n",
       "      <td>1</td>\n",
       "      <td>European multi</td>\n",
       "      <td>[gbm_1, gbm_2]</td>\n",
       "      <td>10.301</td>\n",
       "      <td>EUR</td>\n",
       "      <td>10.301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>multi_pos_5</td>\n",
       "      <td>multi_pos_5</td>\n",
       "      <td>1</td>\n",
       "      <td>European multi</td>\n",
       "      <td>[gbm_1, gbm_2]</td>\n",
       "      <td>10.301</td>\n",
       "      <td>EUR</td>\n",
       "      <td>10.301</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       position          name  quantity            otype      risk_facts  \\\n",
       "0  am_put_pos_0  am_put_pos_0         1  American single         [gbm_1]   \n",
       "1  am_put_pos_1  am_put_pos_1         1  American single         [gbm_1]   \n",
       "2  am_put_pos_2  am_put_pos_2         1  American single         [gbm_1]   \n",
       "3   multi_pos_3   multi_pos_3         1   European multi  [gbm_1, gbm_2]   \n",
       "4   multi_pos_4   multi_pos_4         1   European multi  [gbm_1, gbm_2]   \n",
       "5   multi_pos_5   multi_pos_5         1   European multi  [gbm_1, gbm_2]   \n",
       "\n",
       "    value currency  pos_value  \n",
       "0   3.325      EUR      3.325  \n",
       "1   3.303      EUR      3.303  \n",
       "2   3.275      EUR      3.275  \n",
       "3  10.301      EUR     10.301  \n",
       "4  10.301      EUR     10.301  \n",
       "5  10.301      EUR     10.301  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Portfolio Risk Reports"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Portfolio risk reports are meant to provide a broad overview of how sensitive the value of a portfolio is with regard to the value of certain input parameters (market data, model parameters). While **Greeks** provide the same information with regard to marginal changes in the input paramters, risk reports provide a **wider range input-output (parameter-portfolio value) combinations**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### No Correlation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, consider the portfolio from before, i.e. **without correlation**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0.],\n",
       "       [0., 1.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "portfolio.val_env.get_list('cholesky_matrix')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Calling the method `get_port_risk` and providing a key for the respetive Greek yields sensitivities with regard to all risk factors (here: `gbm_1` and `gbm_2`). "
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "portfolio.valuation_objects[3].underlying_objects['gbm_1'].update(initial_value=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "gbm_2\n",
      "0.8\n",
      "0.9\n",
      "1.0\n",
      "1.1\n",
      "1.2\n",
      "\n",
      "gbm_1\n",
      "0.8\n",
      "0.9\n",
      "1.0\n",
      "1.1\n",
      "1.2\n",
      "\n",
      "\n",
      "\n",
      "CPU times: user 15.6 s, sys: 328 ms, total: 15.9 s\n",
      "Wall time: 4 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "vegas, benchvalue = portfolio.get_port_risk(Greek='Vega',\n",
    "                                fixed_seed=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The return object is a pandas `Panel` object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>gbm_2_Vega</th>\n",
       "      <th>gbm_1_Vega</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dim_0</th>\n",
       "      <th>dim_1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.8</th>\n",
       "      <th>factor</th>\n",
       "      <td>0.400</td>\n",
       "      <td>0.160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>36.357</td>\n",
       "      <td>32.307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.9</th>\n",
       "      <th>factor</th>\n",
       "      <td>0.450</td>\n",
       "      <td>0.180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>38.655</td>\n",
       "      <td>33.246</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.0</th>\n",
       "      <th>factor</th>\n",
       "      <td>0.500</td>\n",
       "      <td>0.200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>40.965</td>\n",
       "      <td>40.965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.1</th>\n",
       "      <th>factor</th>\n",
       "      <td>0.550</td>\n",
       "      <td>0.220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>43.269</td>\n",
       "      <td>35.109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.2</th>\n",
       "      <th>factor</th>\n",
       "      <td>0.600</td>\n",
       "      <td>0.240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>45.570</td>\n",
       "      <td>35.994</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              gbm_2_Vega  gbm_1_Vega\n",
       "dim_0 dim_1                         \n",
       "0.8   factor       0.400       0.160\n",
       "      value       36.357      32.307\n",
       "0.9   factor       0.450       0.180\n",
       "      value       38.655      33.246\n",
       "1.0   factor       0.500       0.200\n",
       "      value       40.965      40.965\n",
       "1.1   factor       0.550       0.220\n",
       "      value       43.269      35.109\n",
       "1.2   factor       0.600       0.240\n",
       "      value       45.570      35.994"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vegas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the helper funtion `risk_report` allows the easy, readable printout of the results, i.e. the **portfolio volatility sensitivities**. In this case you can see that, for example, the increase in the first risk fator's (`gbm_1`) volatility by 10% leads to a portfolio value increase bya bit less than 1 currency unit. Decreasing the same input parameter by 10% reduces the portfolio value by a bit less than 1 currency unit."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "gbm_2_Vega\n",
      "dim_0  dim_1 \n",
      "0.8    factor     0.40\n",
      "       value     36.36\n",
      "0.9    factor     0.45\n",
      "       value     38.66\n",
      "1.0    factor     0.50\n",
      "       value     40.96\n",
      "1.1    factor     0.55\n",
      "       value     43.27\n",
      "1.2    factor     0.60\n",
      "       value     45.57\n",
      "Name: gbm_2_Vega, dtype: float64\n",
      "\n",
      "gbm_1_Vega\n",
      "dim_0  dim_1 \n",
      "0.8    factor     0.16\n",
      "       value     32.31\n",
      "0.9    factor     0.18\n",
      "       value     33.25\n",
      "1.0    factor     0.20\n",
      "       value     40.96\n",
      "1.1    factor     0.22\n",
      "       value     35.11\n",
      "1.2    factor     0.24\n",
      "       value     35.99\n",
      "Name: gbm_1_Vega, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "dx.risk_report(vegas)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Of course, you can generate the same risk report for the **portfolio initial value sensitivities**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "gbm_2\n",
      "0.8\n",
      "0.9\n",
      "1.0\n",
      "1.1\n",
      "1.2\n",
      "\n",
      "gbm_1\n",
      "0.8\n",
      "0.9\n",
      "1.0\n",
      "1.1\n",
      "1.2\n",
      "\n",
      "\n",
      "\n",
      "CPU times: user 15.7 s, sys: 366 ms, total: 16.1 s\n",
      "Wall time: 4.06 s\n"
     ]
    }
   ],
   "source": [
    "%time deltas, benchvalue = portfolio.get_port_risk(Greek='Delta', fixed_seed=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example, increasing the initial value of the first risk factor (`gbm_1`) by 10% increases the portfolio value by about 11 currency units."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>gbm_2_Delta</th>\n",
       "      <th>gbm_1_Delta</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dim_0</th>\n",
       "      <th>dim_1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.8</th>\n",
       "      <th>factor</th>\n",
       "      <td>32.000</td>\n",
       "      <td>32.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>29.841</td>\n",
       "      <td>25.476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.9</th>\n",
       "      <th>factor</th>\n",
       "      <td>36.000</td>\n",
       "      <td>36.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>34.902</td>\n",
       "      <td>28.440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.0</th>\n",
       "      <th>factor</th>\n",
       "      <td>40.000</td>\n",
       "      <td>40.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>40.965</td>\n",
       "      <td>40.965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.1</th>\n",
       "      <th>factor</th>\n",
       "      <td>44.000</td>\n",
       "      <td>44.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>47.964</td>\n",
       "      <td>45.306</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.2</th>\n",
       "      <th>factor</th>\n",
       "      <td>48.000</td>\n",
       "      <td>48.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>55.773</td>\n",
       "      <td>61.356</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              gbm_2_Delta  gbm_1_Delta\n",
       "dim_0 dim_1                           \n",
       "0.8   factor       32.000       32.000\n",
       "      value        29.841       25.476\n",
       "0.9   factor       36.000       36.000\n",
       "      value        34.902       28.440\n",
       "1.0   factor       40.000       40.000\n",
       "      value        40.965       40.965\n",
       "1.1   factor       44.000       44.000\n",
       "      value        47.964       45.306\n",
       "1.2   factor       48.000       48.000\n",
       "      value        55.773       61.356"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deltas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>gbm_2_Delta</th>\n",
       "      <th>gbm_1_Delta</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dim_0</th>\n",
       "      <th>dim_1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0.8</th>\n",
       "      <th>value</th>\n",
       "      <td>-11.124</td>\n",
       "      <td>-15.489</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.9</th>\n",
       "      <th>value</th>\n",
       "      <td>-6.063</td>\n",
       "      <td>-12.525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.0</th>\n",
       "      <th>value</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.1</th>\n",
       "      <th>value</th>\n",
       "      <td>6.999</td>\n",
       "      <td>4.341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.2</th>\n",
       "      <th>value</th>\n",
       "      <td>14.808</td>\n",
       "      <td>20.391</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             gbm_2_Delta  gbm_1_Delta\n",
       "dim_0 dim_1                          \n",
       "0.8   value      -11.124      -15.489\n",
       "0.9   value       -6.063      -12.525\n",
       "1.0   value        0.000        0.000\n",
       "1.1   value        6.999        4.341\n",
       "1.2   value       14.808       20.391"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deltas.loc(axis=0)[:, 'value'] - benchvalue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### With Correlation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider now a **highly negative correlation** case."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "correlations = [['gbm_1', 'gbm_2', -0.9]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "portfolio = dx.derivatives_portfolio(\n",
    "                        'portfolio', positions, val_env,\n",
    "                        risk_factors, correlations, parallel=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.        ],\n",
       "       [-0.9       ,  0.43588989]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "portfolio.val_env.get_list('cholesky_matrix')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since the value of the European maximum call option is dependent on the risk factor correlation you see a **significant change in this derivative's value estimate**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Total\n",
      " pos_value    44.112\n",
      "dtype: float64\n",
      "CPU times: user 2.09 s, sys: 37 ms, total: 2.13 s\n",
      "Wall time: 539 ms\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>position</th>\n",
       "      <th>name</th>\n",
       "      <th>quantity</th>\n",
       "      <th>otype</th>\n",
       "      <th>risk_facts</th>\n",
       "      <th>value</th>\n",
       "      <th>currency</th>\n",
       "      <th>pos_value</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>am_put_pos_0</td>\n",
       "      <td>am_put_pos_0</td>\n",
       "      <td>1</td>\n",
       "      <td>American single</td>\n",
       "      <td>[gbm_1]</td>\n",
       "      <td>3.293</td>\n",
       "      <td>EUR</td>\n",
       "      <td>3.293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>am_put_pos_1</td>\n",
       "      <td>am_put_pos_1</td>\n",
       "      <td>1</td>\n",
       "      <td>American single</td>\n",
       "      <td>[gbm_1]</td>\n",
       "      <td>3.293</td>\n",
       "      <td>EUR</td>\n",
       "      <td>3.293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>am_put_pos_2</td>\n",
       "      <td>am_put_pos_2</td>\n",
       "      <td>1</td>\n",
       "      <td>American single</td>\n",
       "      <td>[gbm_1]</td>\n",
       "      <td>3.293</td>\n",
       "      <td>EUR</td>\n",
       "      <td>3.293</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>multi_pos_3</td>\n",
       "      <td>multi_pos_3</td>\n",
       "      <td>1</td>\n",
       "      <td>European multi</td>\n",
       "      <td>[gbm_1, gbm_2]</td>\n",
       "      <td>11.411</td>\n",
       "      <td>EUR</td>\n",
       "      <td>11.411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>multi_pos_4</td>\n",
       "      <td>multi_pos_4</td>\n",
       "      <td>1</td>\n",
       "      <td>European multi</td>\n",
       "      <td>[gbm_1, gbm_2]</td>\n",
       "      <td>11.411</td>\n",
       "      <td>EUR</td>\n",
       "      <td>11.411</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>multi_pos_5</td>\n",
       "      <td>multi_pos_5</td>\n",
       "      <td>1</td>\n",
       "      <td>European multi</td>\n",
       "      <td>[gbm_1, gbm_2]</td>\n",
       "      <td>11.411</td>\n",
       "      <td>EUR</td>\n",
       "      <td>11.411</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       position          name  quantity            otype      risk_facts  \\\n",
       "0  am_put_pos_0  am_put_pos_0         1  American single         [gbm_1]   \n",
       "1  am_put_pos_1  am_put_pos_1         1  American single         [gbm_1]   \n",
       "2  am_put_pos_2  am_put_pos_2         1  American single         [gbm_1]   \n",
       "3   multi_pos_3   multi_pos_3         1   European multi  [gbm_1, gbm_2]   \n",
       "4   multi_pos_4   multi_pos_4         1   European multi  [gbm_1, gbm_2]   \n",
       "5   multi_pos_5   multi_pos_5         1   European multi  [gbm_1, gbm_2]   \n",
       "\n",
       "    value currency  pos_value  \n",
       "0   3.293      EUR      3.293  \n",
       "1   3.293      EUR      3.293  \n",
       "2   3.293      EUR      3.293  \n",
       "3  11.411      EUR     11.411  \n",
       "4  11.411      EUR     11.411  \n",
       "5  11.411      EUR     11.411  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%time portfolio.get_values(fixed_seed=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Via the `step` parameter, you can influence the  **granularity of the risk report**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "gbm_2\n",
      "0.8\n",
      "0.85\n",
      "0.9\n",
      "0.95\n",
      "1.0\n",
      "1.05\n",
      "1.1\n",
      "1.15\n",
      "1.2\n",
      "\n",
      "gbm_1\n",
      "0.8\n",
      "0.85\n",
      "0.9\n",
      "0.95\n",
      "1.0\n",
      "1.05\n",
      "1.1\n",
      "1.15\n",
      "1.2\n",
      "\n",
      "\n",
      "\n",
      "CPU times: user 25.5 s, sys: 515 ms, total: 26 s\n",
      "Wall time: 6.57 s\n"
     ]
    }
   ],
   "source": [
    "%%time \n",
    "deltas, benchvalue = portfolio.get_port_risk(Greek='Delta',\n",
    "                                 fixed_seed=True,\n",
    "                                 step=0.05)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this case, an increase in the intial value of the first risk factor (`gbm_1`) by 10% leads to a **much higher increase**\n",
    "in the portfolio value of about 15 currency units."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>gbm_2_Delta</th>\n",
       "      <th>gbm_1_Delta</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dim_0</th>\n",
       "      <th>dim_1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.80</th>\n",
       "      <th>factor</th>\n",
       "      <td>32.000</td>\n",
       "      <td>32.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>31.665</td>\n",
       "      <td>27.195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.85</th>\n",
       "      <th>factor</th>\n",
       "      <td>34.000</td>\n",
       "      <td>34.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>34.377</td>\n",
       "      <td>29.649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.90</th>\n",
       "      <th>factor</th>\n",
       "      <td>36.000</td>\n",
       "      <td>36.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>37.365</td>\n",
       "      <td>33.234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">0.95</th>\n",
       "      <th>factor</th>\n",
       "      <td>38.000</td>\n",
       "      <td>38.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>40.617</td>\n",
       "      <td>38.124</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.00</th>\n",
       "      <th>factor</th>\n",
       "      <td>40.000</td>\n",
       "      <td>40.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>44.112</td>\n",
       "      <td>44.112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.05</th>\n",
       "      <th>factor</th>\n",
       "      <td>42.000</td>\n",
       "      <td>42.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>47.820</td>\n",
       "      <td>51.057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.10</th>\n",
       "      <th>factor</th>\n",
       "      <td>44.000</td>\n",
       "      <td>44.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>51.729</td>\n",
       "      <td>58.965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.15</th>\n",
       "      <th>factor</th>\n",
       "      <td>46.000</td>\n",
       "      <td>46.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>55.803</td>\n",
       "      <td>67.641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1.20</th>\n",
       "      <th>factor</th>\n",
       "      <td>48.000</td>\n",
       "      <td>48.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>value</th>\n",
       "      <td>60.021</td>\n",
       "      <td>76.977</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              gbm_2_Delta  gbm_1_Delta\n",
       "dim_0 dim_1                           \n",
       "0.80  factor       32.000       32.000\n",
       "      value        31.665       27.195\n",
       "0.85  factor       34.000       34.000\n",
       "      value        34.377       29.649\n",
       "0.90  factor       36.000       36.000\n",
       "      value        37.365       33.234\n",
       "0.95  factor       38.000       38.000\n",
       "      value        40.617       38.124\n",
       "1.00  factor       40.000       40.000\n",
       "      value        44.112       44.112\n",
       "1.05  factor       42.000       42.000\n",
       "      value        47.820       51.057\n",
       "1.10  factor       44.000       44.000\n",
       "      value        51.729       58.965\n",
       "1.15  factor       46.000       46.000\n",
       "      value        55.803       67.641\n",
       "1.20  factor       48.000       48.000\n",
       "      value        60.021       76.977"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deltas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>gbm_2_Delta</th>\n",
       "      <th>gbm_1_Delta</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>dim_0</th>\n",
       "      <th>dim_1</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0.80</th>\n",
       "      <th>value</th>\n",
       "      <td>-12.447</td>\n",
       "      <td>-16.917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.85</th>\n",
       "      <th>value</th>\n",
       "      <td>-9.735</td>\n",
       "      <td>-14.463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.90</th>\n",
       "      <th>value</th>\n",
       "      <td>-6.747</td>\n",
       "      <td>-10.878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.95</th>\n",
       "      <th>value</th>\n",
       "      <td>-3.495</td>\n",
       "      <td>-5.988</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.00</th>\n",
       "      <th>value</th>\n",
       "      <td>0.000</td>\n",
       "      <td>0.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.05</th>\n",
       "      <th>value</th>\n",
       "      <td>3.708</td>\n",
       "      <td>6.945</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.10</th>\n",
       "      <th>value</th>\n",
       "      <td>7.617</td>\n",
       "      <td>14.853</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.15</th>\n",
       "      <th>value</th>\n",
       "      <td>11.691</td>\n",
       "      <td>23.529</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.20</th>\n",
       "      <th>value</th>\n",
       "      <td>15.909</td>\n",
       "      <td>32.865</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             gbm_2_Delta  gbm_1_Delta\n",
       "dim_0 dim_1                          \n",
       "0.80  value      -12.447      -16.917\n",
       "0.85  value       -9.735      -14.463\n",
       "0.90  value       -6.747      -10.878\n",
       "0.95  value       -3.495       -5.988\n",
       "1.00  value        0.000        0.000\n",
       "1.05  value        3.708        6.945\n",
       "1.10  value        7.617       14.853\n",
       "1.15  value       11.691       23.529\n",
       "1.20  value       15.909       32.865"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "deltas.loc(axis=0)[:, 'value'] - benchvalue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Copyright, License & Disclaimer**\n",
    "\n",
    "© Dr. Yves J. Hilpisch | The Python Quants GmbH\n",
    "\n",
    "DX Analytics (the \"dx library\" or \"dx package\") is licensed under the GNU Affero General\n",
    "Public License version 3 or later (see http://www.gnu.org/licenses/).\n",
    "\n",
    "DX Analytics comes with no representations or warranties, to the extent\n",
    "permitted by applicable law.\n",
    "\n",
    "http://tpq.io | [dx@tpq.io](mailto:team@tpq.io) |\n",
    "http://twitter.com/dyjh\n",
    "\n",
    "<img src=\"http://hilpisch.com/tpq_logo.png\" alt=\"The Python Quants\" width=\"35%\" align=\"right\" border=\"0\"><br>\n",
    "\n",
    "**Quant Platform** | http://pqp.io\n",
    "\n",
    "**Python for Finance Training** | http://training.tpq.io\n",
    "\n",
    "**Certificate in Computational Finance** | http://compfinance.tpq.io\n",
    "\n",
    "**Derivatives Analytics with Python (Wiley Finance)** |\n",
    "http://dawp.tpq.io\n",
    "\n",
    "**Python for Finance (2nd ed., O'Reilly)** |\n",
    "http://py4fi.tpq.io"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
